package com.hex.liyue.usermodule.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.hex.liyue.R;
import com.hex.liyue.commonmodule.fragment.BaseFragment;
import com.hex.liyue.commonmodule.view.DelayButton;
import com.hex.liyue.mainmodule.activity.MainActivity;
import com.hex.liyue.usermodule.presenter.IVcodeLoginView;
import com.hex.liyue.usermodule.presenter.VcodeLoginPresenter;

import butterknife.Bind;
import butterknife.OnClick;


/**
 * 无密码登录的界面
 * Created by yukoyuan on 16/6/14.
 */
public class VCodeLoginFragment extends BaseFragment implements IVcodeLoginView, DelayButton.OnDelayListener {
    @Bind(R.id.iv_toobar_back)
    ImageView ivToobarBack;
    @Bind(R.id.tv_toobar_title)
    TextView tvToobarTitle;
    @Bind(R.id.et_vcode_login_phone)
    EditText etVcodeLoginPhone;
    @Bind(R.id.et_vcode_login_vcode)
    EditText etVcodeLoginVcode;
    @Bind(R.id.db_fg_pwd_getvcode)
    DelayButton dbFgPwdGetvcode;
    private VcodeLoginPresenter vcodeLoginPresenter;

    @Override
    public void initdata(Bundle arguments) {
        dbFgPwdGetvcode.setDelayListener(this);
    }

    @Override
    public int getContentLayout() {
        return R.layout.fragment_vcode;
    }

    @Override
    protected void bindPresenter() {
        vcodeLoginPresenter = new VcodeLoginPresenter(this);
    }

    @Override
    protected void initToolbar() {
        ivToobarBack.setVisibility(View.VISIBLE);
        tvToobarTitle.setVisibility(View.VISIBLE);
        tvToobarTitle.setText("无密码登录");
    }

    @Override
    public String getPhone() {
        return etVcodeLoginPhone.getText().toString().trim();
    }

    @Override
    public String getVcode() {
        return etVcodeLoginVcode.getText().toString().trim();
    }

    @Override
    public void showLod(String msg) {
        showLoad(getActivity(), msg);
    }

    @Override
    public void closeLod() {
        closeLoad();
    }

    @Override
    public void showT(String msg) {
        showToast(getActivity(), msg);
    }


    @Override
    public void startLoginFragment() {
        getActivity().onBackPressed();
    }

    @Override
    public void delyTime(int time) {
        dbFgPwdGetvcode.delay(time);
        dbFgPwdGetvcode.setEnabled(false);
    }

    @Override
    public void startMainActivity() {
        Intent intent=new Intent(getActivity(), MainActivity.class);
        startActivity(intent);
    }

    @OnClick({R.id.iv_toobar_back, R.id.db_fg_pwd_getvcode, R.id.bt_vcode_login_pwd})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_toobar_back:
                startLoginFragment();
                break;
            case R.id.db_fg_pwd_getvcode:
                vcodeLoginPresenter.getVcode();
                break;
            case R.id.bt_vcode_login_pwd:
                vcodeLoginPresenter.login();
                break;

        }
    }


    @Override
    public void onStartDelay(int second) {
        dbFgPwdGetvcode.setEnabled(false);
    }

    @Override
    public void onRun(int second) {

    }

    @Override
    public void onFinish() {
        if (dbFgPwdGetvcode != null) {
            dbFgPwdGetvcode.setEnabled(true);
        }
    }
}
